package fr.nts.application.novadmin.database.dao;

import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import fr.nts.application.novadmin.database.model.employe.NovadminEmployeSimple;
import fr.nts.framework.database.jpa.dao.AJPADao;

/**
 * Un DAO manipulant les {@link NovadminEmployeSimple} avec JPA
 * 
 * @author sco
 * @version 1.0.0
 */
@Component
public class NovadminEmployeeSimpleDao extends AJPADao<NovadminEmployeSimple, Long>{

	private final static transient Logger log = LoggerFactory.getLogger(NovadminEmployeeSimpleDao.class);
	
	private final static String LOGIN = "identifiantEmployee";
	
	/*
	 * (non-Javadoc)
	 * @see fr.nts.framework.database.jpa.dao.AJPADao#getBoClass()
	 */
	@Override
	protected Class<NovadminEmployeSimple> getBoClass(){
		return NovadminEmployeSimple.class;
	}
	

	/**
	 * Récupère un employee Novadmin simple via son login
	 * 
	 * @param login Login de l'employee novadmin
	 * @return Un employee {@link NovadminEmployeSimple}
	 */
	public NovadminEmployeSimple findByLogin(String login){
		NovadminEmployeSimple employee = null;
		
    	CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    	CriteriaQuery<NovadminEmployeSimple> query = builder.createQuery(getBoClass());
    	
    	Root<NovadminEmployeSimple> root = query.from(getBoClass());
    	query.where(builder.equal(root.get(LOGIN), 
    			                  login));
    	
        try{
        	employee = getEntityManager().createQuery(query)
                                         .getSingleResult();
        }catch(NoResultException e){
			// non trouvé
		}
        
        return employee;
	}
	
	
	// GETTER / FLUX
	public Logger getLog(){
		return log;
	}
}